package com.zmj.sy.mom.srv.aps.controller;

import com.zmj.sy.mom.srv.aps.bean.vo.apsorder.*;
import com.zmj.sy.mom.srv.aps.bean.vo.base.*;
import com.zmj.sy.mom.srv.aps.service.ApsOrderService;
import com.zmj.sy.mom.srv.aps.service.OrderService;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@RequiredArgsConstructor
@Api(tags = "apsOrder")
@RestController
@RequestMapping("/apsOrder")
public class ApsOrderController {

    private final ApsOrderService apsOrderService;

    private final OrderService orderService;

    @ApiOperation("预排产列表")
    @GetMapping("/preList")
    public BaseListResVo<ApsOrderPreListResVo> preList(ApsOrderPreListReqVo reqVo){
        return apsOrderService.preList(reqVo);
    }

    @ApiOperation("开始排产")
    @PostMapping("/schedule")
    public BaseResVo schedule(@RequestBody ApsOrderScheduleReqVo reqVo){
        reqVo.setStartDateTime(LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.of(0, 0, 0)));

        try {
            // 回填
            apsOrderService.fillIds(reqVo);
            // 锁定
            apsOrderService.lock(reqVo);
            // 排产
            apsOrderService.schedule(reqVo, null);

            // 如果抛异常，则给未排产的解锁
            apsOrderService.unlock(reqVo, 5);
        } catch (Exception e) {
            apsOrderService.unlock(reqVo, 3);
            e.printStackTrace();
        }
        return BaseResVo.ok();
    }

    @Deprecated
    @ApiOperation("排产发布")
    @PostMapping("/publish")
    public BaseResVo publish(@RequestBody ApsOrderPublishReqVo reqVo){
        apsOrderService.publish(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("已发布订单")
    @GetMapping("/pubList")
    public BaseListResVo<ApsOrderPubListResVo> pubList(ApsOrderPubListReqVo reqVo){
        return apsOrderService.pubList(reqVo);
    }

    @Deprecated
    @ApiOperation("锁定生产订单")
    @PostMapping("/lock")
    public BaseResVo lock(ApsOrderPublishReqVo reqVo){
        apsOrderService.lock(reqVo);
        return BaseResVo.ok();
    }

    @Deprecated
    @ApiOperation("解锁生产订单")
    @PostMapping("/unlock")
    public BaseResVo unlock(ApsOrderPublishReqVo reqVo){
        apsOrderService.unlock(reqVo);
        return BaseResVo.ok();
    }

    @Deprecated
    @ApiOperation("发布回退")
    @PostMapping("/pubFallback")
    public BaseResVo pubFallback(BaseIdsReqVo reqVo){
        orderService.pubFallback(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("查询一个订单下的包")
    @GetMapping("/subOrder")
    public BaseSelectResVo subOrder(BaseIdReqVo reqVo){
        return apsOrderService.subOrder(reqVo);
    }

    @ApiOperation("单订单甘特图")
    @GetMapping("/orderGanttChart")
    public BaseListResVo<ApsOrderGanttChartResVo> orderGanttChart(ApsOrderOrderGanttChartReqVo reqVo){
        return apsOrderService.orderGanttChart(reqVo);
    }

    @ApiOperation("总排产明细甘特图")
    @GetMapping("/ganttChart")
    public BaseListResVo<ApsOrderGanttChartResVo> ganttChart(ApsOrderGanttChartReqVo reqVo){
        return apsOrderService.ganttChart(reqVo);
    }

    @ApiOperation("排产订单选择框")
    @GetMapping("/select")
    public BaseSelectResVo select(){
        return apsOrderService.select();
    }


    @ApiOperation("总工作组负荷情况")
    @GetMapping("/groupLoad")
    public BaseListResVo<ApsOrderGroupLoadResVo> groupLoad(ApsOrderGroupLoadReqVo reqVo){
        if(reqVo.getStartDate() == null){
            throw SyExceptionUtils.e("请传入开始时间");
        }

        if(reqVo.getEndDate() == null){
            throw SyExceptionUtils.e("请传入结束时间");
        }
        return apsOrderService.groupLoad(reqVo);
    }

    @ApiOperation("总订单甘特图")
    @GetMapping("/groupGanttChart")
    public BaseListResVo<ApsOrderGroupGanttChartResVo> groupGanttChart(ApsOrderGroupGanttChartReqVo reqVo){
        return apsOrderService.groupGanttChart(reqVo);
    }

    @ApiOperation("根据当前工单详情查找所属工单的工单详情列表")
    @GetMapping("/currentWorkDetail")
    public BaseListResVo<ApsOrderCurrentWorkDetailResVo> currentWorkDetail(BaseIdReqVo reqVo){
        return apsOrderService.currentWorkDetail(reqVo);
    }

    @ApiOperation("编辑拆包的到期日")
    @PostMapping("/editEndDate")
    public BaseResVo editEndDate(@RequestBody ApsOrderEditEndDateReqVo reqVo){
        apsOrderService.editEndDate(reqVo);
        return BaseResVo.ok();
    }

}
